package com.blockmeta.bbs.immodule.widget;

import android.content.Context;
import androidx.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.blockmeta.bbs.baselibrary.base.app.BaseApp;
import com.blockmeta.bbs.businesslibrary.net.pojo.BaseImResponse;
import com.blockmeta.bbs.businesslibrary.net.retrofit.RetrofitFactory;
import com.blockmeta.bbs.businesslibrary.net.rxjava.CommonSubscriber;
import com.blockmeta.bbs.businesslibrary.net.rxjava.RxUtil;
import com.blockmeta.bbs.businesslibrary.util.UserSharedPreferenceUtil;
import com.blockmeta.bbs.immodule.Constants;
import com.blockmeta.bbs.immodule.R;
import com.blockmeta.bbs.immodule.corebean.ICircleDomainService;
import com.blockmeta.bbs.immodule.corebean.Impl.IMApplicationContext;
import com.blockmeta.bbs.immodule.net.api.GetNewNotice;
import com.blockmeta.bbs.immodule.net.api.ReadNotice;
import com.blockmeta.bbs.immodule.net.pojo.Notice;
import com.trello.rxlifecycle2.components.support.RxAppCompatActivity;

public class NoticeBar extends LinearLayout {
    Context mContext;

    TextView mtvContent;

    ImageView mIvClose;

    ICircleDomainService circleDomainService;

    long mCircleId;


    public NoticeBar(Context context) {
        super(context);
        init(context);
    }

    public NoticeBar(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public NoticeBar(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }

    private void init(final Context context){
        inflate(context, R.layout.view_notice_bar,this);
        circleDomainService = IMApplicationContext.getCircleDomainService();
        mtvContent = (TextView)findViewById(R.id.tv_notice_content);
        mIvClose = (ImageView) findViewById(R.id.iv_close);
    }

    public void onInitialize(Context context, final long toUserId, boolean refreshNotice) {
        mContext = context;
        mCircleId = toUserId;
        if(!refreshNotice){
            final Notice notice = circleDomainService.getNoticeInMem(mCircleId);
            if(notice == null){
                getNoticeAndShow();
            }else {
                judgeNeedShow(notice);
            }

        }else {
            getNoticeAndShow();
        }

    }

    private void getNoticeAndShow() {
        RetrofitFactory.getRootRetrofit().create(GetNewNotice.class)
                .getNewNotice(mCircleId)
                .compose(((RxAppCompatActivity)mContext).<BaseImResponse<Notice>>bindToLifecycle())
                .compose(RxUtil.<BaseImResponse<Notice>>rxSchedulerHelper())
                .compose(RxUtil.<Notice>dataNullResult())
                .subscribe(new CommonSubscriber<BaseImResponse<Notice>>(mContext) {

                    @Override
                    public void onNext(BaseImResponse<Notice> result) {
                        if (result == null || result.data == null) {
                            return;
                        }
                        circleDomainService.putNoticeInMem(result.data, mCircleId);
                        judgeNeedShow(result.data);
                    }
                });
    }

    private void judgeNeedShow(final Notice notice) {

        //获得这个圈子这个用户读过的最近一条公告的id
        int noticeId = UserSharedPreferenceUtil.getInt(BaseApp.getApp(), Constants.READ_NOTICE_ID_LONG + mCircleId, 0);
        if(notice.getId() == noticeId){
            setVisibility(GONE);
        }else {
            setVisibility(VISIBLE);
            mtvContent.setText("圈子公告：" + notice.getContent());
            mtvContent.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    Popup_Circle_Notice popup_circle_notice = new Popup_Circle_Notice(mContext, notice);
                    popup_circle_notice.showPop();
                    //发送阅读公告
                    RetrofitFactory.getRootRetrofit().create(ReadNotice.class)
                            .readNotice(notice.getId())
                            .compose(RxUtil.dataNullResult())
                            .compose(RxUtil.<BaseImResponse<Object>>rxSchedulerHelper())
                            .subscribe(new CommonSubscriber<BaseImResponse<Object>>(mContext) {
                                @Override
                                public void onNext(BaseImResponse<Object> o) {
                                    UserSharedPreferenceUtil.putInt(BaseApp.getApp(), Constants.READ_NOTICE_ID_LONG + mCircleId, notice.getId());
                                }
                            });
                    setVisibility(GONE);
                }
            });
            mIvClose.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    setVisibility(GONE);
                    UserSharedPreferenceUtil.putInt(BaseApp.getApp(), Constants.READ_NOTICE_ID_LONG + mCircleId, notice.getId());
                }
            });

        }
    }

}
